const userService = require("../services/userService");
const result = require("../utils/result");

module.exports = {
  // 创建用户（注册）
  async createUser(req, res) {
    try {
      const user = req.body;
      const response = await userService.createUser(user);
      console.log(response, user);
      return res.json(result.success(response));
    } catch (error) {
      console.log(error);
      return res.json(result.fail(error.message));
    }
  },

  // 查询单个用户通过手机号（登录）
  async getUserByPhone(req, res) {
    try {
      const { phone, password } = req.query;
      const response = await userService.getUserByPhone(phone, password);
      return res.json(result.success(response));
    } catch (error) {
      return res.json(result.fail(error.message));
    }
  },

  // 更新用户信息
  async updateUser(req, res) {
    try {
      const user = req.body;
      const response = await userService.updateUser(user);
      return res.json(result.success(response));
    } catch (error) {
      return res.json(result.fail(error.message));
    }
  },

  // 获取所有用户
  async getAllUser(req, res) {
    try {
      const response = await userService.getAllUser();
      return res.json(result.success(response));
    } catch (error) {
      return res.json(result.fail(error.message));
    }
  },

  // 删除用户
  async deleteUser(req, res) {
    try {
      const { id } = req.query;
      const response = await userService.deleteUser(id);
      return res.json(result.success(response));
    } catch (error) {
      return res.json(result.fail(error.message));
    }
  },
};
